package com.atguigu.eduservice.controller;


import com.alibaba.excel.EasyExcel;
import com.atguigu.common.R;
import com.atguigu.eduservice.entity.excel.ExcelSubjectData;
import com.atguigu.eduservice.entity.subject.OneSubjectVo;
import com.atguigu.eduservice.handler.excel.SubjectExcelListener;
import com.atguigu.eduservice.service.EduSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-11
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description="Eaxcel上传和读取")
@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService subjectService;

    //课程分类列表
    @GetMapping("getAllSubject")
    @ApiOperation(value = "课程分类列表")
    public R getAllSubject() {
        List<OneSubjectVo> list = subjectService.getAllEduSubject();
        return R.ok().data("allSubject",list);
    }

    //把excel上传过来，进行读取
    @PostMapping("addSubject")
    @ApiOperation(value = "上传excel进行读取")
    public R addSubject(MultipartFile file) {
        //得到上传文件
        try {
            //使用EasyExcel进行读取
            InputStream in = file.getInputStream();
            EasyExcel.read(in, ExcelSubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
            return R.ok();
        }catch(Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}

